<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ComponentClass
 *
 * @author Administrador
 */
class XMLTableGridClass {

    //put your code here
    private $numCol;
    private $numRow;
    private $tableTitle;
    private $tableGrid;
    private $divID;
    private $xmlFile;
    private $xmlFileURL;
    private $tableColumnHeads;
    private $columns;
    private $content;

    function __construct($divID,$title,$xmlFileURL) {
        $this->tableTitle=$title;
        $this->setTableGrid($divID);
        $this->columns = array();
        $this->tableColumnHeads= "";
        $this->content = "";
        $this->setXMLFile($xmlFileURL);
    }

    public static function LIB($path) {
        echo " <link rel=\"stylesheet\" type=\"text/css\" href=\"../bootstrap/css/DT_bootstrap.css\">
        <style type=\"text/css\" title=\"currentStyle\">
            @import \"" . $path . "component/datagrid/media/css/demo_page.css\"; 
            @import \"" . $path . "component/datagrid/media/css/header.css\";
            @import \"" . $path . "component/datagrid/media/css/demo_table.css\";
        </style>
        <script type=\"text/javascript\" language=\"javascript\" src=\"" . $path . "component/datagrid/media/js/jquery.js\"></script>
        <script type=\"text/javascript\" language=\"javascript\" src=\"" . $path . "component/datagrid/media/js/jquery.dataTables.js\"></script>";
    }

    private function setTableGrid($divID) {
        $this->divID=$divID;
        $this->tableGrid = " 
            <script type=\"text/javascript\" charset=\"utf-8\">
            var asInitVals = new Array();

            $(document).ready(function() {
                var oTable = $('#" . $this->divID . "').dataTable({
                    \"oLanguage\": {
                        \"sSearch\": \"Search all columns:\"
                    }
                });


            });
        </script>";

        echo "" . $this->tableGrid;
    }

    private function setXMLFile($url) {
        $this->setXMLFileURL($url);
        $this->xmlFile = simplexml_load_file($this->xmlFileURL) or die("no load");
    }

    private function setXMLFileURL($url) {
        $this->xmlFileURL = $url;
    }
    public function addXMLTableColumnHead($tableHead){
        $this->tableColumnHeads=$this->tableColumnHeads."<th>". $tableHead."</th>";
    }

    public function tableXMLContent() {

        $this->numRow = count($this->columns[0]);
        $this->numCol = count($this->columns);

        $row = 0;
        while ($row < $this->numRow) {
            $this->content = $this->content . "<tr>";

            for ($col = 0; $col < $this->numCol; $col++) {

                $this->content = $this->content . '<td>' . $this->columns[$col][$row] . '</td>';
            }
            $this->content = $this->content . " </tr>";
            $row++;
        }

        return $this->content;
    }

    public function addXMLTableColumn($xmlXPathQuery) {
        $xmlColumn = $this->xmlFile->xpath($xmlXPathQuery);
        array_push($this->columns, $xmlColumn);
    }

    public function showXMLTable() {
        echo "<div class=\"panel panel-default\">
                <div class=\"panel-heading\">
                    <h3 class=\"panel-title\">".$this->tableTitle."</h3>
                </div>
                <div class=\"panel-body\">
                    <div class=\"small\">
                        <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" 
                        class=\"table table-striped table-bordered table-condensed table-responsiv\" id=\"".$this->divID."\">
                            <thead>
                                <tr>
                                   ".$this->tableColumnHeads." 
                                </tr>
                            </thead>
                            <tbody>    
                                     ".$this->tableXMLContent()."
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>";
    }

}
